package example.java.lang.annotation;


/**
 * An annotation type declaration and include defaults.
 */

import java.lang.annotation.*;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno{
	String str();
	int val();
}


public class Meta {
	public static void main(String[] args) {
		myMeth();
	}
	
	
	// Annotate a method. 
	@MyAnno(str = "Annotation Example", val = 100)
	public static void myMeth(){
		Meta ob = new Meta();
		
		//获得Annotation注解的方法, 并显示注解的值
		try {
			
			//首先，获得一个表示这个类的Class对象。
			Class c = ob.getClass();
			
			//获得代表这个方法的Method对象
			Method m = c.getMethod("myMeth", null);

			//然后获得Annotation对象
			MyAnno anno = m.getAnnotation(MyAnno.class);
			
			//最后显示Annotation的值
			System.out.println(anno.str() + ": " + anno.val());
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
}
